package com.yang.jd.task;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yang.jd.pojo.Item;
import com.yang.jd.service.ItemService;
import com.yang.jd.util.HttpUitls;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.net.www.http.HttpClient;

import java.util.Date;
import java.util.List;

/****
 *  @Author: 无为
 *  @Data: 2021/8/28 21:33
 */
@Component
public class ItemTask {

    @Autowired
    private HttpUitls httpUitls;

    @Autowired
    private ItemService itemService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    //当下载任务完成后，间隔多长时间进行下次的任务。
    @Scheduled(fixedDelay = 100*1000 )
    public void itemTask() throws JsonProcessingException {
        //声明需要解析的初始地址
        String url = "http://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA";

        //按照页面对手机的搜索结果进行遍历解析
        for (int i = 1; i < 10; i=i+2) {
            String html = httpUitls.doGetHtml(url + i);
            //解析页面，获取商品数据并存储
            this.parse(html);
        }

        System.out.println("手机数据爬取完成！");
    }

    /***
     *  解析页面，获取商品数据并存储
     * @param html
     */
    private void parse(String html) throws JsonProcessingException {
        //解析html获取Document
        Document doc = Jsoup.parse(html);
        System.out.println(doc);
        //获取spu
        Elements spuEles = doc.select("div#J_goodsList > ul > li");
        for (Element spuEle : spuEles) {
            //获取spu
            String attr = spuEle.attr("data-spu");
            Long spu = Long.parseLong(attr.equals("")?"0":attr);


            //获取sku信息
            Elements skuEles = spuEle.select("li.ps-item");

            for (Element skuEle : skuEles) {
                //获取sku
                long sku = Long.parseLong(skuEle.select("[data-sku]").attr("data-sku"));

                //根据sku查询商品数据
                Item item = new Item();
                item.setSku(sku);
                List<Item> list = this.itemService.findAll(item);

                if(list.size() > 0){
                    //如果商品存在，就进行下一个循环，该商品不保存，因为已存在
                    continue;
                }

                //设置商品的spu
                item.setSpu(spu);

                //获取商品的详情的url
                String itemUrl = "https://item.jd.com/" + sku + ".html";
                item.setUrl(itemUrl);

                //获取商品的图片
                String picUrl = skuEle.select("img[data-sku]").first().attr("data-lazy-img");
                // 图片路径可能会为空的情况
                if(!StringUtils.isNotBlank(picUrl)){
                    picUrl = skuEle.select("img[data-sku]").first().attr("data-lazy-img-slave");
                }
                picUrl ="https:" +  picUrl.replace("/n7/" ,"/n1/"); //替换图片格式
                String picName = this.httpUitls.doGetImage(picUrl);
                item.setPic(picName);

                //获取商品的价格
                //http://p.3.cn/prices/mgets?skuIds=J_100022447596%2CJ_100011863279%2CJ_100023014936%2CJ_100011920605
                String priceJson = this.httpUitls.doGetHtml("http://p.3.cn/prices/mgets?skuIds=J_" + sku);
                double price = MAPPER.readTree(priceJson).get(0).get("m").asDouble();
                item.setPrice(price);

                //获取商品的标题
                String itemInfo = this.httpUitls.doGetHtml(item.getUrl());
                String title = Jsoup.parse(itemInfo).select("div.sku-name").text();
                item.setTitle(title);

                item.setCreated(new Date());
                item.setUpdated(item.getCreated());

                //保存商品数据到数据库中
                this.itemService.save(item);
            }

        }

    }
}
